
import Communication.MyResultSet;
import java.io.*;
import java.net.Socket;

/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
/**
 *
 * @author TrongKhoa
 */
public class Client implements Runnable
{

    frmClient _parent;
    MyConnectionString _connection;
    boolean _running;
    boolean _connected;
    Socket _sqlSocket;
    ObjectInputStream _ois;
    ObjectOutputStream _oos;

    public Client(frmClient parent, MyConnectionString connectionString)
    {
        _parent = parent;
        _connection = connectionString;
        _connected = false;
    }

    @Override
    public void run()
    {
        try
        {
            System.out.println("Client running");
            _running = true;
            do
            {
                // Client connect tới
                if (Connect())
                {
                    System.out.print("Valid user!");
                    Thread.sleep(200);

                    _connected = true;


                    // Khi đã hợp lệ, tạo ra socket khác, connect vs server để gửi câu query đi
                    _sqlSocket = new Socket(_connection._ServerIP, Integer.parseInt(_connection._ServerPort));



                    while (_connected)
                    {
                        Thread.sleep(2000);
                        System.out.print("Tao ngủ chút");
                    }
                }
            }
            while (_running);
            _connected = false;
        }
        catch (Exception ex)
        {
        }
    }

    public void Exit()
    {
        try
        {
            _connected = false;
            _running = false;
            _ois.close();
            _oos.close();
            _sqlSocket.close();
        }
        catch (Exception e)
        {
        }
    }

    private boolean Connect()
    {
        try
        {
            // Tạo socket
            Socket socket = new Socket(_connection._ServerIP, Integer.parseInt(_connection._ServerPort));
            ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());

            //Gửi đi connection
            oos.writeUTF(_connection.GetConnectionString());
            System.out.println("Send connection string: " + _connection.GetConnectionString());
            oos.flush();

            ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
            String message = (String) ois.readUTF();

            oos.close();
            ois.close();
            socket.close();

            if (message.equals("Valid"))
            {
                // Hợp lệ
                return true;
            }
            else
            {
                return false;
            }
        }
        catch (Exception ex)
        {
            return false;
        }
    }

    // Hàm này chịu trách nhiệm gửi câu query đi & xử lý kq trả về
    public void ExecuteQuery(String query)
    {
        if (_connected)
        {
            try
            {
                if (_oos == null)
                {
                    _oos = new ObjectOutputStream(_sqlSocket.getOutputStream());
                }

                _oos.writeUTF(query);
                System.out.println("Đã gửi đi câu query:" + query);
                _oos.flush();

                if (_ois == null)
                {
                    _ois = new ObjectInputStream(_sqlSocket.getInputStream());
                }
                MyResultSet result = (MyResultSet) _ois.readObject();
                System.out.println("Đã nhận kết quả!");
                if (result._HasError)
                {
                    _parent._queryPanel.AddMessage(result._ErrorMessage);
                }
                else
                {
                    _parent._queryPanel.AddResult(result);
                }

            }
            catch (Exception ex)
            {
            }

        }
    }
}
